package com.controller;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.annotation.ExcelResponse;
import com.config.TableProperties;
import com.entity.vo.DataColumnCheckExportVo;
import com.entity.vo.DataTableCheckExportVo;
import com.entity.dto.TableCheck;
import com.entity.param.MySQLSchemaParam;
import com.service.DataQualityService;
import com.service.MySqlSchemaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Api(tags = "数据质量-数据检查")
@RestController
public class CheckController {
    @Resource
    private MySqlSchemaService mySqlSchemaService;

    @Resource
    private DataQualityService dataQualityService;

    @Resource
    private TableProperties tableProperties;


    @ExcelResponse(dataTarget = DataTableCheckExportVo.class,downloadName = "质量检测结果",suffix = ExcelType.HSSF)
    @ApiOperation(value = "表质量检测")
    @GetMapping("/tableCheck")
    public List<DataTableCheckExportVo> tableCheck(){
        List<TableCheck> tableChecks = tableProperties.getTableChecks();
        List<DataTableCheckExportVo> res = new LinkedList<>();
        tableChecks.forEach(e->{
            MySQLSchemaParam param = new MySQLSchemaParam();
            param.setTableName(e.getTableName());
            param.setDatabaseName(e.getDataBase());
            Long tableCount = mySqlSchemaService.queryCount(param);
            mySqlSchemaService.queryList(param);
            DataTableCheckExportVo exportVo = new DataTableCheckExportVo();
            exportVo.setTableName(param.getTableName());
            exportVo.setTableComment(e.getComment());
            exportVo.setTableRows(tableCount);
            exportVo.setRemark(param.getDatabaseName());
            res.add(exportVo);
        });
        return res;
    }

    private final ExecutorService threadPool = Executors.newFixedThreadPool(3);
    @ExcelResponse(dataTarget = DataColumnCheckExportVo.class,downloadName = "字段质量检测结果",suffix = ExcelType.HSSF)
    @ApiOperation(value = "表列检测")
    @GetMapping("/columnCheck")
    public List<DataColumnCheckExportVo> columnCheck(){

        List<DataColumnCheckExportVo> checkExportVoList = new CopyOnWriteArrayList<>();
        List<TableCheck> tableChecks = tableProperties.getTableChecks();
        log.info("开始列检查");
        List<CompletableFuture<Void>> taskFutures = new LinkedList<>();
        tableChecks.forEach(e->{
            CompletableFuture<Void> taskFuture = CompletableFuture.runAsync(() -> {
                dataQualityService.toCheck(checkExportVoList, e);
            }, threadPool);
            taskFutures.add(taskFuture);
        });

        CompletableFuture.allOf(taskFutures.toArray(new CompletableFuture[]{})).join();
        checkExportVoList.sort(Comparator.comparing(DataColumnCheckExportVo::getTableName));
        return checkExportVoList;
    }

    @ExcelResponse(dataTarget = DataColumnCheckExportVo.class,downloadName = "人口库-字段质量检测结果",suffix = ExcelType.HSSF)
    @ApiOperation(value = "人口库-表列检测")
    @GetMapping("/personColumnCheck")
    public List<DataColumnCheckExportVo> personColumnCheck(){
        List<DataColumnCheckExportVo> checkExportVoList = new CopyOnWriteArrayList<>();
        List<TableCheck> tableChecks = tableProperties.getPersonChecks();
        log.info("开始列检查");
        tableChecks.forEach(e->{
            dataQualityService.personCheck(checkExportVoList, e);
        });
        checkExportVoList.sort(Comparator.comparing(DataColumnCheckExportVo::getTableName));
        return checkExportVoList;
    }

}
